package com.yeyks.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.hotel.HotelRecommendCityAddParam;
import com.yeyks.common.param.hotel.HotelRecommendCityUpdateParam;
import com.yeyks.common.utils.LocationUtils;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.common.utils.webutil.CityUtils;
import com.yeyks.hotel.dal.dao.HotelRecommendCityMapper;
import com.yeyks.hotel.dal.domain.HotelRecommendCity;
import com.yeyks.hotel.service.HotelRecommendCityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 推荐城市表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2020-03-02
 */
@Service
public class HotelRecommendCityServiceImpl extends ServiceImpl<HotelRecommendCityMapper, HotelRecommendCity> implements HotelRecommendCityService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertCity(HotelRecommendCityAddParam param) {

        HotelRecommendCity city = ObjectConvertUtil.convertObject(param, HotelRecommendCity.class);
        //得到经纬度
        CityUtils cityUtils = LocationUtils.fromBaiDu(param.getCityName());
        if(null != cityUtils && cityUtils.getResult() != null && cityUtils.getResult().getLocation() != null){
            //存入经纬度
            city.setLnt(String.valueOf(cityUtils.getResult().getLocation().getLng()));
            city.setLat(String.valueOf(cityUtils.getResult().getLocation().getLat()));
        }
        city.setCreateTime(new Date());
        city.setUpdateTime(new Date());
        int insert = baseMapper.insert(city);
        return insert > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateCity(HotelRecommendCityUpdateParam param) {
        //检查ID是否存在
        HotelRecommendCity city = baseMapper.selectById(param.getId());
        if(city == null){
            throw new ServiceException("主键ID不存在");
        }
        city = ObjectConvertUtil.convertObject(param, HotelRecommendCity.class);
        city.setUpdateTime(new Date());
        //得到经纬度
        CityUtils cityUtils = LocationUtils.fromBaiDu(param.getCityName());
        if(null != cityUtils && cityUtils.getResult() != null && cityUtils.getResult().getLocation() != null){
            //存入经纬度
            city.setLnt(String.valueOf(cityUtils.getResult().getLocation().getLng()));
            city.setLat(String.valueOf(cityUtils.getResult().getLocation().getLat()));
        }
        int update = baseMapper.updateById(city);
        return update > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean syncCityLat() {
        List<HotelRecommendCity> hotelRecommendCities = baseMapper.selectList(new QueryWrapper<HotelRecommendCity>());
        if(!CollectionUtils.isEmpty(hotelRecommendCities)){
            hotelRecommendCities.forEach(hotelRecommendCity -> {
                CityUtils cityUtils = LocationUtils.fromBaiDu(hotelRecommendCity.getCityName());
                if(null != cityUtils && cityUtils.getResult() != null && cityUtils.getResult().getLocation() != null){
                    //存入经纬度
                    hotelRecommendCity.setLnt(String.valueOf(cityUtils.getResult().getLocation().getLng()));
                    hotelRecommendCity.setLat(String.valueOf(cityUtils.getResult().getLocation().getLat()));
                }
            });
            //批量更新
            return super.updateBatchById(hotelRecommendCities);
        }
        return false;
    }

    @Override
    public List<HotelRecommendCity> enableList(Integer hot) {
        List<HotelRecommendCity> hotelRecommendCities = baseMapper.selectList(new QueryWrapper<HotelRecommendCity>()
                .eq(HotelRecommendCity.OPEN_STATUS,1)
                .eq(Objects.nonNull(hot), HotelRecommendCity.TYPE, hot));
        return hotelRecommendCities;
    }
}
